const conf = {
  properties: {},
  data: {
    hasEmptyGrid: false,
    point: []
  },
  methods: {
    getThisMonthDays(year, month) {
      return new Date(year, month, 0).getDate();
    },
    getFirstDayOfWeek(year, month) {
      return new Date(Date.UTC(year, month - 1, 1)).getDay();
    },
    calculateEmptyGrids(year, month) {
      const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
      var yearEmpty = month == 1 ? year - 1 : year;
      var monthEmpty = month == 1 ? 12 : month - 1;
      var num;
      const firstDayOfEmpty = this.getThisMonthDays(yearEmpty, monthEmpty);
      let empytGrids = [];
      if (firstDayOfWeek > 0) {
        for (let i = 0; i < firstDayOfWeek; i++) {
          var emptyI = firstDayOfEmpty - firstDayOfWeek + i + 1;
          empytGrids.push({
            index: emptyI,
            show: false,
            date: yearEmpty + "-" + (monthEmpty * 1 < 10 ? "0" + monthEmpty : monthEmpty) + "-" + emptyI,
          });
        }
        this.setData({
          hasEmptyGrid: true,
          empytGrids
        });
      } 
      else {
        this.setData({
          hasEmptyGrid: false,
          empytGrids: []
        });
      }
     
    },
    linemun(){
     
      var newdate = new Date(new_year, new_month, 1);//第一天 
      var daye = (now(newdate.getTime() - 1000 * 60 * 60 * 24)).getDate();//最后一天
      var now = new Date(y, (m - 1), d);
      var nowDayOfWeek = now.getDay();
    },
    lineDate(point) {
      if (!point || point.length == 0) return;
      var pushNum = 0;
      var lowIndex = -1;
      var dd = this.data.days;
      var ed = this.data.empytGrids;
      var td = this.data.tGrids;
      for (var i = 0; i < dd.length; ++i) {
        dd[i].show = false;
        dd[i].fow = false;
        dd[i].low = false;
      }
      for (var i = 0; i < ed.length; ++i) {
        ed[i].show = false;
        ed[i].fow = false;
        ed[i].low = false;
      }
      for (var i = 0; i < td.length; ++i) {
        td[i].show = false;
        td[i].fow = false;
        td[i].low = false;
      }
      for (var i = 0, len = ed.length; i < len; ++i) {
        for (var j = 0, plen = point.length; j < plen; j++) {
          if (point[j] == ed[i].date) {
            ed[i].show = true;
            if (pushNum == 0) ed[i].fow = true;
            pushNum++;
          }
        }
      }
      var radiusIndex = 0;
      for (var i = 0, len = dd.length; i < len; ++i) {
        for (var j = 0, plen = point.length; j < plen; j++) {
          if (point[j] == dd[i].date) {
            dd[i].show = true;
            if (pushNum == 0) dd[i].fow = true;
            pushNum++
            if (pushNum == 7 || i == len - 1){
               dd[i].low = true;
               radiusIndex = i;
            }
          }
        }
      }
      for (var i = 0, len = td.length; i < len; ++i) {
        for (var j = 0, plen = point.length; j < plen; j++) {
          if (point[j] == td[i].date) {
            dd[radiusIndex].low = false;
            td[i].show = true;
            if (pushNum == 0) td[i].fow = true;
            pushNum++
            if (pushNum == 7 || i == len - 1) td[i].low = true;
          }
        }
      }
      this.setData({
        empytGrids: ed,
        days: dd,
        tGrids:td
      });
    },
    isSmallMonth(mon) {
      if ( mon == 4 || mon == 6 || mon == 9 || mon == 11) return true;
      else return false;
    },
    thisWeek(y, m, d) {
      var today = new Date();
      today.setUTCFullYear(y);
      today.setUTCMonth(m - 1);
      today.setUTCDate(d);
      var week = today.getDay();
   
      // if (((y % 4) == 0) && ((y % 100) != 0) || ((y % 400) == 0)) {
      //   if (m == 2) week = week == 0 ? 6 : week-1;
      // } 
      // else if (this.isSmallMonth(m)) {
      //   week = week - 2 < 0 ? week + 5 : week - 2;
      // }
      var offset = d - week;
      var nm = m;
      var ny = y;
      console.log("week", week);
      if (offset <= 0) {
        nm = m - 1 <= 0 ? 12 : m - 1;
        if (nm == 12) ny--;
      }
      m = parseInt(m)
      nm = parseInt(nm)
      nm = nm < 10 ? "0" + nm : nm;
      m = m < 10 ? "0" + m : m;
      var ndays = this.getThisMonthDays(y, nm);
      var points = [];
      console.log("offset",offset);
      for (var i = 0, j = offset; i < 7; ++i, j++) {
        if (j <= 0) points.push(ny + "-" + nm + "-" + (ndays + j));
        else {
          var day = i + offset;
          if (day <= ndays) {
            points.push(y + "-" + m + "-" + (day < 10 ? "0" + day : day));
          }

        }
      }
      if (points.length<7){
        var cm = parseInt(m) + 1, cy = parseInt(y);
        if(m==12){
          cm = 1;
          cy++;
        }
        cm = parseInt(cm);
        cm = cm < 10 ? "0" + cm:cm;
        for (var i = 1, len = 7-points.length; i <= len; ++i) {
            points.push(cy + "-" + cm + "-" + "0" + i );
        }
      }
      console.log("thisWeek", points);
      return points;
    },
    calculateDays(year, month) {
      let days = [];
      let tGrids = [];
      month = parseInt(month);
      const thisMonthDays = this.getThisMonthDays(year, month);
      for (let i = 1; i <= thisMonthDays; i++) {
        var dt = {
          index: i,
          date: year + "-" + (month * 1 < 10 ? "0" + month : month) + "-" + (i < 10 ? "0" + i : i),
          show: false
         
        }
        days.push(dt);
      }
      var ed = this.data.empytGrids;
      var totalDayNum = ed.length + days.length > 35 ? 6 * 7 : 5 * 7;
      var moreDay = totalDayNum - thisMonthDays - this.data.empytGrids.length + 1;

      console.log("totalDayNum",totalDayNum);
      var moreMonth = month+1>12?1:month+1;
      for (let i = 1; i < moreDay;++i){
        var dt = {
          index: i,
          date: year + "-" + (moreMonth * 1 < 10 ? "0" + moreMonth : moreMonth) + "-" + (i < 10 ? "0" + i : i),
          show: false
        }
        tGrids.push(dt);
      }
      this.setData({
        days, tGrids
      });
    },
    pointDate(point) {
      if (!point || point.length == 0) return;
      var dd = this.data.days;
      for (var i = 0, len = dd.length; i < len; ++i) {
        for (var j = 0, plen = point.length; j < plen; j++) {
          if (point[j] == dd[i].date) {
            dd[i].show = true;
          }
        }
      }
      this.setData({
        days: dd
      });
    },
    handleCalendar(year, month,renderAfter) {
      // const handle = e.currentTarget.dataset.handle;
      // const cur_year = this.data.cur_year;
      // const cur_month = this.data.cur_month;
      // month = month.length < 2?"0"+ month:month;
      // year = 2020;
      if (true) {
        let newMonth = month;
        let newYear = year;
        // let newMonth = cur_month - 1;
        // let newYear = cur_year;
        // if (newMonth < 1) {
        //   newYear = cur_year - 1;
        //   newMonth = 12;
        // }

        this.calculateEmptyGrids(newYear, newMonth);
        this.calculateDays(newYear, newMonth);

        this.setData({
          cur_year: newYear,
          cur_month: newMonth
        }, renderAfter)
        var eventDetail = {
          year: newYear,
          month: newMonth
        } // detail对象，提供给事件监听函数
        var eventOption = {} // 触发事件的选项
        this.triggerEvent('changeday', eventDetail, eventOption)

      } else {
        let newMonth = month;
        let newYear = year;
        // let newMonth = cur_month + 1;
        // let newYear = cur_year;
        // if (newMonth > 12) {
        //   newYear = cur_year + 1;
        //   newMonth = 1;
        // }

        this.calculateEmptyGrids(newYear, newMonth);
        this.calculateDays(newYear, newMonth);

        this.setData({
          cur_year: newYear,
          cur_month: newMonth
        }, renderAfter)
        var eventDetail = {
          year: newYear,
          month: newMonth
        } // detail对象，提供给事件监听函数
        var eventOption = {} // 触发事件的选项
        this.triggerEvent('changeday', eventDetail, eventOption)
      }

    },
    toactive: function (e) {
      var cent = e.target.dataset.cent;
      var cents = e.currentTarget.dataset.cent;
      var myEventDetail = { cents } // detail对象，提供给事件监听函数
      var myEventOption = {} // 触发事件的选项
      this.triggerEvent('myevent', myEventDetail, myEventOption)

    }

  },
  attached(options) {
    const date = new Date();
    const cur_year = date.getFullYear();
    const cur_month = date.getMonth() + 1;
    const cur_day = date.getDate();
    const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
    this.calculateEmptyGrids(cur_year, cur_month);
    this.calculateDays(cur_year, cur_month);
    this.setData({
      cur_year,
      cur_month,
      weeks_ch,
      cur_day
    })
  }


};

Component(conf);
